<template>
  <div class="demo-container">
    <div class="demo-header">
      <h1>图片上传功能演示</h1>
      <p>这是一个完整的图片上传组件演示，支持拖拽上传、预览、删除等功能</p>
    </div>

    <div class="demo-content">
      <div class="upload-section">
        <h2>营业执照上传</h2>
        <p class="section-desc">请上传清晰的营业执照照片，支持 JPG、PNG 格式，大小不超过 5MB</p>
        
        <ImageUpload
          :image-url="businessLicense"
          @update:image-url="handleBusinessLicenseUpdate"
          :accept="'image/*'"
          :size-limit="5"
          :upload-hint="'支持 JPG、PNG 格式，大小不超过 5MB'"
        />

        <div v-if="businessLicense" class="upload-result">
          <h3>✅ 上传成功</h3>
          <p><strong>文件大小：</strong>{{ getFileSize(businessLicense) }}</p>
          <p><strong>上传时间：</strong>{{ uploadTime }}</p>
          <el-button size="small" @click="downloadImage(businessLicense, '营业执照')">下载图片</el-button>
        </div>
      </div>

      <div class="upload-section">
        <h2>身份证上传</h2>
        <p class="section-desc">请上传清晰的身份证照片，支持 JPG、PNG 格式，大小不超过 2MB</p>
        
        <ImageUpload
          :image-url="idCard"
          @update:image-url="handleIdCardUpdate"
          :accept="'image/*'"
          :size-limit="2"
          :upload-hint="'支持 JPG、PNG 格式，大小不超过 2MB'"
        />

        <div v-if="idCard" class="upload-result">
          <h3>✅ 上传成功</h3>
          <p><strong>文件大小：</strong>{{ getFileSize(idCard) }}</p>
          <el-button size="small" @click="downloadImage(idCard, '身份证')">下载图片</el-button>
        </div>
      </div>

      <div class="demo-actions">
        <el-button @click="clearAll" :disabled="!businessLicense && !idCard">
          清空所有图片
        </el-button>
        <el-button type="primary" @click="submitImages" :disabled="!businessLicense && !idCard">
          提交图片
        </el-button>
        <el-button type="success" @click="previewAll" :disabled="!businessLicense && !idCard">
          预览所有图片
        </el-button>
      </div>

      <div v-if="showPreview" class="preview-section">
        <h2>图片预览</h2>
        <div class="preview-grid">
          <div v-if="businessLicense" class="preview-item">
            <h3>营业执照</h3>
            <img :src="businessLicense" alt="营业执照" class="preview-image" />
          </div>
          <div v-if="idCard" class="preview-item">
            <h3>身份证</h3>
            <img :src="idCard" alt="身份证" class="preview-image" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import ImageUpload from '@/components/common/ImageUpload.vue'

// 响应式数据
const businessLicense = ref('')
const idCard = ref('')
const uploadTime = ref('')
const showPreview = ref(false)

// 处理营业执照上传
const handleBusinessLicenseUpdate = (imageUrl) => {
  businessLicense.value = imageUrl
  if (imageUrl) {
    uploadTime.value = new Date().toLocaleString()
    ElMessage.success('营业执照上传成功！')
  }
}

// 处理身份证上传
const handleIdCardUpdate = (imageUrl) => {
  idCard.value = imageUrl
  if (imageUrl) {
    ElMessage.success('身份证上传成功！')
  }
}

// 获取文件大小
const getFileSize = (base64String) => {
  if (!base64String) return '0 KB'
  
  const sizeInBytes = (base64String.length * 3) / 4
  const sizeInKB = (sizeInBytes / 1024).toFixed(2)
  const sizeInMB = (sizeInKB / 1024).toFixed(2)
  
  if (sizeInMB > 1) {
    return `${sizeInMB} MB`
  } else {
    return `${sizeInKB} KB`
  }
}

// 下载图片
const downloadImage = (base64String, filename) => {
  const link = document.createElement('a')
  link.href = base64String
  link.download = `${filename}_${Date.now()}.png`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  ElMessage.success(`${filename}下载成功！`)
}

// 清空所有图片
const clearAll = () => {
  ElMessageBox.confirm('确定要清空所有已上传的图片吗？', '确认清空', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    businessLicense.value = ''
    idCard.value = ''
    uploadTime.value = ''
    showPreview.value = false
    ElMessage.success('所有图片已清空')
  })
}

// 提交图片
const submitImages = () => {
  const images = []
  if (businessLicense.value) images.push('营业执照')
  if (idCard.value) images.push('身份证')
  
  ElMessage.success(`成功提交 ${images.join('、')} 图片！`)
}

// 预览所有图片
const previewAll = () => {
  showPreview.value = !showPreview.value
  if (showPreview.value) {
    ElMessage.info('显示图片预览')
  } else {
    ElMessage.info('隐藏图片预览')
  }
}
</script>

<style scoped>
.demo-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  background: #f5f7fa;
  min-height: 100vh;
}

.demo-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 30px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.demo-header h1 {
  color: #2c3e50;
  margin: 0 0 15px 0;
  font-size: 32px;
  font-weight: 700;
}

.demo-header p {
  color: #7f8c8d;
  font-size: 16px;
  margin: 0;
  line-height: 1.6;
}

.demo-content {
  display: grid;
  gap: 30px;
}

.upload-section {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.upload-section h2 {
  color: #2c3e50;
  margin: 0 0 10px 0;
  font-size: 24px;
  font-weight: 600;
}

.section-desc {
  color: #7f8c8d;
  margin: 0 0 25px 0;
  font-size: 14px;
  line-height: 1.5;
}

.upload-result {
  margin-top: 20px;
  padding: 20px;
  background: #f0f9ff;
  border-radius: 8px;
  border-left: 4px solid #409eff;
}

.upload-result h3 {
  color: #67c23a;
  margin: 0 0 15px 0;
  font-size: 16px;
}

.upload-result p {
  color: #606266;
  margin: 8px 0;
  font-size: 14px;
}

.demo-actions {
  text-align: center;
  padding: 30px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.demo-actions .el-button {
  margin: 0 10px;
}

.preview-section {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.preview-section h2 {
  color: #2c3e50;
  margin: 0 0 25px 0;
  font-size: 24px;
  font-weight: 600;
}

.preview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

.preview-item {
  text-align: center;
}

.preview-item h3 {
  color: #409eff;
  margin: 0 0 15px 0;
  font-size: 18px;
}

.preview-image {
  max-width: 100%;
  max-height: 300px;
  border: 2px solid #e4e7ed;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.preview-image:hover {
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .demo-container {
    padding: 15px;
  }
  
  .demo-header {
    padding: 20px;
  }
  
  .demo-header h1 {
    font-size: 24px;
  }
  
  .upload-section {
    padding: 20px;
  }
  
  .demo-actions .el-button {
    margin: 5px;
    width: 100%;
    max-width: 200px;
  }
  
  .preview-grid {
    grid-template-columns: 1fr;
  }
}
</style>
